package com.nerve.secondhandcar.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Length;
import org.ko.core.domain.auth.User;
import org.ko.core.domain.support.BaseEntity;

import com.nerve.secondhandcar.domain.car.CarCategory;
import com.nerve.secondhandcar.domain.car.Configure;
import com.nerve.secondhandcar.domain.report.Report;


/**
 * 平台中的汽车对象Domain
 * @author 集成显卡
 * @version 2014-10-09 10:51:08
 */
@SuppressWarnings("serial")
@Entity
@Table(name="car")
public class Car extends BaseEntity {
	/**
	 * 待售
	 */
	public static final int PENDING = 0;
	/**
	 * 已售
	 */
	public static final int SALED = 1;
	
	private User user;					//发布者
	private Brand brand;				//汽车品牌
	private String name;				//型号名称，如：1.4L手动豪华版
	private String fullName;			//完整的名称，如：2009款 大众 捷达 1.4L手动豪华版
	private float price;
	private float freshPrice;			//新车参考价格
	private String belong;				//归属地
	private String plateNumber;			//车牌号
	private String regYear;				//车辆年份
	private String checkDate;			//年检到期
	private String plateDate;			//上牌时间
	private String mileage;				//行驶里程
	private int clickAmount;			//浏览数
	private Date postDate;				//发布时间
	private int trash;					//是否删除，为0是正常
	private int specialPrice; 			//是否为特价， 如果是特价，则在首页的特价区显示
	private int status;					//状态，0=代售，1=已售
	
	private String information;		//车辆说明文字	add on 2015年12月15日15:29:46
	private String alias;			//车辆别名		add on 2015年12月15日15:29:56

	//车辆配置
	private Configure configure;
	
	//车辆测评报告
	private Report report;
	
	private List<Picture> pictures;		//车辆图片
	
	private CarCategory category;
	
	/**
	 * 获取汽车的封面
	 * 返回的是第一张图片
	 * @method name: getFirstPicture
	 * @return type: String
	 *	@return
	 */
	@Transient
	public String getFirstPicture(){
		if(getPictures().isEmpty()){
			return null;
		}
		return getPictures().get(0).getPath();
	}
	
	@ManyToOne(fetch = FetchType.LAZY,cascade={CascadeType.REFRESH})
	@JoinColumn(name = "category_id")
	public CarCategory getCategory() {
		return category;
	}
	public void setCategory(CarCategory category) {
		this.category = category;
	}

	@OneToMany(mappedBy="appId", fetch=FetchType.LAZY)
	@Where(clause="category='CAR'")
	@OrderBy("sort desc,id asc")
	public List<Picture> getPictures() {
		return pictures;
	}

	public void setPictures(List<Picture> pictures) {
		this.pictures = pictures;
	}

	@OneToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
	@JoinColumn(name="report_id", insertable=true)
	public Report getReport() {
		return report;
	}

	public void setReport(Report report) {
		this.report = report;
	}

	@OneToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
	@JoinColumn(name="configure_id", insertable=true)
	public Configure getConfigure() {
		return configure;
	}

	public void setConfigure(Configure configure) {
		this.configure = configure;
	}

	public Car() {
		super();
	}

	public Car(Long id){
		this();
		this.setId(id);
	}

	
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "brand_id", nullable = true)
	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user_id", nullable = true)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(columnDefinition="float(10,2)")
	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	@Column(columnDefinition="float(10,2)")
	public float getFreshPrice() {
		return freshPrice;
	}

	public void setFreshPrice(float freshPrice) {
		this.freshPrice = freshPrice;
	}

	public String getBelong() {
		return belong;
	}

	public void setBelong(String belong) {
		this.belong = belong;
	}

	public String getPlateNumber() {
		return plateNumber;
	}

	public void setPlateNumber(String plateNumber) {
		this.plateNumber = plateNumber;
	}

	public String getRegYear() {
		return regYear;
	}

	public void setRegYear(String regYear) {
		this.regYear = regYear;
	}

	public String getCheckDate() {
		return checkDate;
	}

	public void setCheckDate(String checkDate) {
		this.checkDate = checkDate;
	}

	public String getPlateDate() {
		return plateDate;
	}

	public void setPlateDate(String plateDate) {
		this.plateDate = plateDate;
	}

	public String getMileage() {
		return mileage;
	}

	public void setMileage(String mileage) {
		this.mileage = mileage;
	}

	public int getClickAmount() {
		return clickAmount;
	}

	public void setClickAmount(int clickAmount) {
		this.clickAmount = clickAmount;
	}

	public Date getPostDate() {
		return postDate;
	}

	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}

	@Column(length=1)
	public int getTrash() {
		return trash;
	}

	public void setTrash(int trash) {
		this.trash = trash;
	}
	public int getSpecialPrice() {
		return specialPrice;
	}
	public void setSpecialPrice(int specialPrice) {
		this.specialPrice = specialPrice;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	@Lob 
	@Basic(fetch = FetchType.LAZY) 
	@Length(max=100000)
	public String getInformation() {
		return information;
	}

	public void setInformation(String information) {
		this.information = information;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}
}